package com.soilflow.web.controller.reception.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soilflow.common.annotation.Log;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.SysDept;
import com.soilflow.common.core.domain.entity.SysDictData;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.*;
import com.soilflow.common.domain.dto.*;
import com.soilflow.common.domain.result.TimeBack;
import com.soilflow.common.domain.vo.*;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.*;
import com.soilflow.framework.websocket.WebSocketUsers;
import com.soilflow.info.service.*;
import com.soilflow.system.domain.SysNotice;
import com.soilflow.system.service.ISysDeptService;
import com.soilflow.system.service.ISysDictTypeService;
import com.soilflow.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 土地信息Controller
 * 
 * @author relief
 * @date 2022-10-19
 */
@RestController
@RequestMapping("/land/reception")
public class ReliefLandReceptionController extends BaseController
{
    @Autowired
    private LandInfoService landInfoService;
    @Autowired
    private LandAuctionService landAuctionService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private LandClassifyService soilflowClassifyService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private LandNoticeService landNoticeService;

    @Autowired
    private LandAuctionEnrollService landAuctionEnrollService;

    @Autowired
    private LandNewsService landNewsService;

    @Autowired
    private LandLawService landLawService;

    @Autowired
    private LandAuctionPriceService landAuctionPriceService;

    @Autowired
    private LandCarouselChartService landCarouselChartService;
    /**
     * 查询土地前台信息列表
     */
//    @PreAuthorize("@ss.hasPermi('land:info:list')")
    @GetMapping("/info/list")
    public TableDataInfo list(LandInfoDto landInfo)
    {
        startPage();
        List<LandInfoVo> list = landInfoService.selectAuctionLandInfoList(landInfo);
        return getDataTable(list);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 查询分类列表
     */
    @GetMapping("/classify/list")
    public AjaxResult list(LandClassifyDto dto) {
        List<LandClassifyVo> list = soilflowClassifyService.pageList(dto);
        return AjaxResult.success(list);
    }


    /**
     * 获取镇
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysDept dept)
    {
        dept.setParentId(100L);
        List<SysDept> depts = deptService.selectList(dept);

        return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
    }

    /**
     * 获取竞拍大厅详情
     */
    @GetMapping("/auction/{id}")
    public AjaxResult deatil(@PathVariable Long id)
    {
        LandInfoVo landInfoVo = landInfoService.selectLandInfoByAuctionId(id);
        //        //当竞拍时间不为空时
        if (!ObjectUtils.isEmpty(landInfoVo)&&landInfoVo.getAuctionStart()!=null&&landInfoVo.getAuctionEnd()!=null){
            String getdao = DateUtils.getDatePoo2(landInfoVo.getAuctionEnd(),DateUtils.getTime(),landInfoVo.getPriceNow());
            WebSocketUsers.sendMessageToUsersByText(getdao);
        }
        return AjaxResult.success(landInfoVo);
    }

    /**
     * 竞拍大厅当前用户最新报价
     */
    @GetMapping("/price/{id}")
    public AjaxResult userPrice(@PathVariable Long id){

        QueryWrapper<LandAuctionPrice> landAuctionPriceQueryWrapper = new QueryWrapper<>();
        landAuctionPriceQueryWrapper.lambda().eq(LandAuctionPrice::getUserId,SecurityUtils.getUserId())
                .eq(LandAuctionPrice::getAuctionId,id)
                .last("limit 1");
        LandAuctionPrice landAuctionPrice = landAuctionPriceService.getOne(landAuctionPriceQueryWrapper);
        System.out.println("landAuctionPrice = " + landAuctionPrice);
        return AjaxResult.success(landAuctionPrice);
    }


    /**
     * 新闻咨询列表
     * @param dto
     * @return
     */
    @GetMapping("/news/list")
    public TableDataInfo newsList(LandNewsDto dto)
    {
        startPage();
        List<LandNewsVo> list = landNewsService.newsList(dto);
        return getDataTable(list);
    }

    /**
     * 新闻详情
     * @return
     */
    @GetMapping("/news/detail/{id}")
    public AjaxResult getLandNewsById(@PathVariable Long id){
        LandNewsVo vo = landNewsService.getLandNewsById(id);
        return AjaxResult.success(vo);
    }


    /**
     * 通知公告列表
     * @param
     * @return
     */
    @GetMapping("/notice/list")
    public TableDataInfo noticeList(LandNoticeDto notice)
    {
        startPage();
        List<LandNoticeVo> list = landNoticeService.noticeList(notice);
        return getDataTable(list);
    }

    /**
     * 通知公告列表
     * @param
     * @return
     */
    @GetMapping("/notice/detail/{id}")
    public AjaxResult noticedetail(@PathVariable Long id)
    {
        startPage();
        LandNotice vo = landNoticeService.getLandNoticeById(id);
        return AjaxResult.success(vo);
    }

    /**
     * 通知公告列表
     * @param
     * @return
     */
    @GetMapping("/show/list")
    public TableDataInfo showList(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectList(notice);
        return getDataTable(list);
    }

    /**
     * 通知公告列表
     * @param
     * @return
     */
    @GetMapping("/show/detail/{id}")
    public AjaxResult showdetail(@PathVariable Long id)
    {
        SysNotice vo = noticeService.selectNoticeById(id);
        return AjaxResult.success(vo);
    }
    /**
     * 添加
     * @param dto
     * @return
     */
    @PostMapping("/enroll/add")
    public AjaxResult AddAuctionEnrollInfo(@RequestBody LandAuctionEnrollDto dto){
        System.out.println("dto = " + dto);
        Boolean result  = landAuctionEnrollService.AddAuctionEnrollInfo(dto);
        if (result){
            return AjaxResult.success("报名成功");
        }else {
            return AjaxResult.error("你已报名该项目，请勿重复报名");
        }
    }

    /**
     * 法律列表
     * @param dto
     * @return
     */
    @GetMapping("/law/list")
    public TableDataInfo lawList(LandLawDto dto)
    {
        startPage();
        List<LandLawVo> list = landLawService.lawList(dto);
        return getDataTable(list);
    }

    /**
     * 法律详情
     * @return
     */
    @GetMapping("/law/detail/{id}")
    public AjaxResult getLandLawById(@PathVariable Long id){
        LandLawVo vo = landLawService.getLandLawById(id);
        return AjaxResult.success(vo);
    }

    /**
     * 竞拍详情
     * @return
     */
    @GetMapping("/auction/detail/{infoId}")
    public AjaxResult getLandLawByInfoId(@PathVariable Long infoId){
        LandAuctionVo auctionInfoById = landAuctionService.getAuctionInfoByInfoId(infoId);
        return AjaxResult.success(auctionInfoById);
    }

    /**
     * 回显详情
     * @return
     */
    @GetMapping("/carouse")
    public AjaxResult getLandCarouseChartById(){
        List<LandCarouselChart> list = landCarouselChartService.list();
        LandCarouselChart landCarouselChart=new LandCarouselChart();
        if (list.size()>0){
            landCarouselChart = list.get(0);
        }
        return AjaxResult.success(landCarouselChart);
    }
    @GetMapping("/carouse2")
    public AjaxResult getLandCarouseChartById2(){
        List<LandCarouselChart> list = landCarouselChartService.list();
        LandCarouselChart landCarouselChart=new LandCarouselChart();
        if (list.size()>0){
            landCarouselChart = list.get(1);
        }
        return AjaxResult.success(landCarouselChart);
    }
    /**
     * 我的购买记录
     * @return
     */
    @GetMapping("/my")
    public TableDataInfo getMyAuction(LandAuctionEnrollDto dto){
        System.out.println("dto = " + dto);
        List<LandAuctionEnrollVo> list = landAuctionEnrollService.myAuctionEnrollInfoList(dto);
        return getDataTable(list);
    }

    /**
     * 查询土地前台信息列表
     */
//    @PreAuthorize("@ss.hasPermi('land:info:list')")
    @GetMapping("/project/list")
    public TableDataInfo listRecetion(LandInfoDto landInfo)
    {
        startPage();
        System.out.println(landInfo);
        List<LandInfoVo> list = landInfoService.selectRecetionLandInfoList(landInfo);
        return getDataTable(list);
    }

    /**
     * 根据镇统计土地数量
     */
    @GetMapping("/totalByTown")
    public AjaxResult totalByTown() {
        List<TotalByTown> totalByTowns = landInfoService.totalByTown();
        return AjaxResult.success(totalByTowns);
    }
    /**
     * 统计村、社区土地数量
     */
    @GetMapping("/toVillage")
    public AjaxResult toVillage() {
        List<TotalByTown> totalByTowns = landInfoService.toVillage();
        return AjaxResult.success(totalByTowns);
    }
}